package Gui.Dialogs;

import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JScrollPane;

import Gui.GameFrame;
import Gui.GamePanel;
import Main.Defines;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ViewLoansDialog extends JDialog {

	private JLabel currentLoanLabel;
	private JLabel currentCashLabel;
	private int originalCash;
	private int originalLoan;
	private final static int LOAN_AMOUNT = 1000;
	private void buildUIContents(){
		originalCash = Defines.CP().getCash();
		originalLoan = Defines.CP().getLoan();
		currentLoanLabel = new JLabel("Current Loan Amount: $"
				+ Defines.CP().getLoan() + "    "); 
		// TODO fix bug when loan hits 10k
		
		currentCashLabel = new JLabel(String.format("Player %d Cash: %d",
				Defines.CP().getIDPrintable(),
				Defines.CP().getCash()));
		
		JButton loan = new JButton(String.format("Loan $%d from Bank",LOAN_AMOUNT));
		JButton payback = new JButton(String.format("Payback $%d to Bank",LOAN_AMOUNT));
		JButton ok = new JButton("OK");
		JButton cancel = new JButton("Cancel");
		loan.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				Defines.CP().AddLoan(LOAN_AMOUNT);
				Defines.CP().AddCash(LOAN_AMOUNT);
				update();
			}
		});
		payback.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO check if player can afford payback
				// also check if player owes at least loan amount
				
				if(Defines.CP().getCash() >= LOAN_AMOUNT && Defines.CP().getLoan() >= LOAN_AMOUNT){
					Defines.CP().AddLoan(-LOAN_AMOUNT);
					Defines.CP().AddCash(-LOAN_AMOUNT);
				}
				update();
		
			}});
		ok.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {


				// TODO setChangeInCash for loan amount
				dispose();
				GameFrame.getInstance().update();
			}
		});

		cancel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				Defines.CP().setCash(originalCash);
				Defines.CP().setLoan(originalLoan);
				GamePanel.getInstance().update();
				dispose();
				GameFrame.getInstance().update();
			}
		});

		add(currentLoanLabel);
		add(currentCashLabel);
		add(loan);
		add(payback);
		add(ok);
		add(cancel);
	}
	public ViewLoansDialog(JFrame frame) {

		super(frame, "View Loans", true);
		setLayout(new GridLayout(6, 1, 10, 10)); // MAGIC NUMBER
		setLocationRelativeTo(frame);
		
		buildUIContents();
		
		pack();
		// REFACTOR THIS! MAGIC NUMBER
		if(getTitle() != null){
			FontMetrics fm = getFontMetrics(getFont());
			int width = fm.stringWidth(getTitle()) + 75; // MAGIC NUMBER, 75 is size of a "typical" window icon
			width = (int) Math.max(width*1.2, getPreferredSize().getWidth());
			setSize(new Dimension(width, getHeight()));
		}

		setVisible(true);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}

	private void update() {
		GameFrame.getInstance().update();
		currentCashLabel.setText(String.format("Player %d Cash: %d", Defines
				.CP().getIDPrintable(),
				Defines.CP().getCash()));
		currentLoanLabel.setText("Current Loan Amount: $"
				+ Defines.CP().getLoan());

	}
}
